using System;
using System.Collections.Generic;
using PropertyExpression.Common;

namespace PropertyExpression.ReactiveRelation
{
    public static partial class Value
    {
        public static IValue<TTarget> Convert<TSource, TTarget>(
            this IValue<TSource> value, Func<TSource, TTarget> func)
        {
            return new ValueConverter<TSource, TTarget>(value, func);
        }

        private class ValueConverter<TSource, TTarget> : IValue<TTarget>
        {
            private readonly IValue<TSource> value;
            private readonly Func<TSource, TTarget> func;

            public ValueConverter(IValue<TSource> value, Func<TSource, TTarget> func)
            {
                this.value = value;
                this.func = func;
            }

            public IOptionalResult<TTarget> GetValue(IPropertySettor propertySettor, 
                IOneToManyCollectionNewItemProcessor oneToManyCollectionNewItemProcessor)
            {
                return value.GetValue(propertySettor, oneToManyCollectionNewItemProcessor).Execute(func);
            }

            public IEnumerable<ICell> GetCells()
            {
                return value.GetCells();
            }
        }
    }
}
